/*
 * @Author: liaoyu1992 1519778181@qq.com
 * @Date: 2022-11-03 23:06:55
 * @LastEditors: liaoyu1992 1519778181@qq.com
 * @LastEditTime: 2022-11-05 10:57:03
 * @FilePath: \kgantt\core\helpers\otherHelper.ts
 * @Description: 其它工具
 */
import { BarTask } from "../types/barTask";
import { Task } from "../types/publicTypes";
export function isKeyboardEvent(
    event: MouseEvent | KeyboardEvent | FocusEvent
): event is KeyboardEvent {
    return (event as KeyboardEvent).key !== undefined;
}

export function isMouseEvent(
    event: MouseEvent | KeyboardEvent | FocusEvent
): event is MouseEvent {
    return (event as MouseEvent).clientX !== undefined;
}
export function isBarTask(task: Task | BarTask): task is BarTask {
    return (task as BarTask).x1 !== undefined;
}

export function removeHiddenTasks(tasks: Task[]) {
    const groupedTasks = tasks.filter(
        t => t.hideChildren && t.type === "project"
    );
    if (groupedTasks.length > 0) {
        for (let i = 0; groupedTasks.length > i; i++) {
            const groupedTask = groupedTasks[i];
            const children = getChildren(tasks, groupedTask);
            tasks = tasks.filter(t => children.indexOf(t) === -1);
        }
    }
    return tasks;
}

function getChildren(taskList: Task[], task: Task) {
    let tasks: Task[] = [];
    if (task.type !== "project") {
        tasks = taskList.filter(
            t => t.dependencies && t.dependencies.indexOf(task.id) !== -1
        );
    } else {
        tasks = taskList.filter(t => t.project && t.project === task.id);
    }
    var taskChildren: Task[] = [];
    tasks.forEach(t => {
        taskChildren.push(...getChildren(taskList, t));
    })
    tasks = tasks.concat(tasks, taskChildren);
    return tasks;
}

export const sortTasks = (taskA: Task, taskB: Task) => {
    const orderA = taskA.displayOrder || Number.MAX_VALUE;
    const orderB = taskB.displayOrder || Number.MAX_VALUE;
    if (orderA > orderB) {
        return 1;
    } else if (orderA < orderB) {
        return -1;
    } else {
        return 0;
    }
};
